<template>
  <div class="pre-book-history">
    <el-row :gutter="10">
      <el-col :span="24">
        <el-card class="box-card mt-15">
          <div class="home-form">
            <el-form ref="ruleForm" label-width="60px" @submit.native.prevent>
              <el-row :gutter="10">
                <el-col :span="3">
                  <el-form-item label="店铺" prop="storeId">
                    <el-select v-model="ruleForm.storeId" clearable placeholder="请选择" style="width:100%" size="mini">
                      <el-option
                        v-for="item in shopArr"
                        :key="item.id"
                        :label="item.storeName"
                        :value="item.id">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="日期" prop="dateData">
                    <el-date-picker
                      style="width: 100%"
                      size="mini"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      v-model="ruleForm.dateData"
                      type="datetimerange"
                      range-separator="至"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期">
                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="4">
                  <el-form-item label="会员" prop="key">
                    <el-input  placeholder="请输入会员手机号" size="mini" v-model.trim="ruleForm.key" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="4">
                  <el-button type="primary" icon="el-icon-search" size="mini" @click="handleSearch" class="search-btn">搜索</el-button>
                  <el-button type="warning" icon="el-icon-search" size="mini" @click="handleResetForm" class="search-btn">重置</el-button>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </el-card>
        <el-card class="box-card">
          <!-- 头部 -->
          <div slot="header" class="clearfix fix-lh">
            <span>预约历史</span>
          </div>
          <div class="home-body">
            <cvue-table
              :option="tableOption"
              :data="tableData"
              ref="tbl1"
              :isShowAdd="false"
              :page="page"
              :tablePage="tablePage"
              :pageSize="page.pageSize"
              :loading="tableLoading"
              @current-change="handleCurrentChange"
              @size-change="handleSizeChange"
              @select-change="handleSelectChange"
              :isShowPage="true">
              <template slot-scope="scope" slot="classStartTime">
                <span>{{ scope.row.classStartTime + ' - ' + scope.row.classEndTime }}</span>
              </template>
              <template slot-scope="scope" slot="appointmentWay">
                <span>{{ scope.row.appointmentWay === 0 ? '教练代约' : '会员预约' }}</span>
              </template>
              <template slot-scope="scope" slot="appointmentStatus">
                <span>{{ setAppointmentStatus(scope.row.appointmentStatus) }}</span>
              </template>
            </cvue-table>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import cvueTable from '@/components/cvue-table'
export default {
  name: 'prebook-history',
  components: {
    cvueTable
  },
  data () {
    return {
      tablePage: 1,
      tableLoading: false,
      page: {
        total: 1, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10, // 每页显示多少条
        pageSizes: [10, 50, 100]
      },
      tableOption: {
        stripe: true,
        width: '100%',
        border: true,
        index: false,
        selected: false,
        cloumn: [
          { prop: 'index', label: '', align: 'center', width: 58 },
          { prop: 'memberUserName', label: '会员', align: 'center', sortable: true },
          // { prop: 'phone', label: '电话', align: 'center', sortable: true },
          { prop: 'courseSnapshotName', label: '课程名称', align: 'center', sortable: true },
          { prop: 'coachUserName', label: '教练', align: 'center', sortable: true },
          { prop: 'createTime', label: '预约时间', align: 'center', sortable: true },
          { prop: 'classStartTime', label: '上课时间', align: 'center', sortable: true, slotStatus: true, width: 280 },
          { prop: 'appointmentWay', label: '预约方式', align: 'center', sortable: true, slotStatus: true },
          { prop: 'appointmentStatus', label: '状态', align: 'center', sortable: true, slotStatus: true }
        ]
      },
      tableData: [
        // { name: 'ddd', code: 'sss', order: 1, status: 1 }
      ],
      dialogIndexEdit: false,
      shopArr: [],
      ruleForm: {
        storeId: '',
        dateData: [],
        key: ''
      },
      selectForm: {
        storeId: '',
        dateData: [],
        key: ''
      }
    }
  },
  created () {
    this.handleList(this.ruleForm.storeId, this.selectForm.dateData[0], this.selectForm.dateData[1], this.selectForm.key)
    this.getShops()
  },
  methods: {
    setAppointmentStatus (val) {
      if (val === -1) {
        return '已作废'
      } else if (val === 0) {
        return '已预约'
      } else if (val === 1) {
        return '已完成'
      } else if (val === 2) {
        return '会员取消'
      } else if (val === 3) {
        return '教练取消'
      }
    },
    // 获取店铺
    getShops () {
      this.$store.dispatch('manage/StoreGet', '').then(res => {
        // console.log(res)
        if (res.code == '200') {
          var data = res.data
          this.shopArr = data
        }
      })
    },
    handleResetForm () {
      // this.$refs['ruleForm'].resetFields()
      this.$set(this.ruleForm, 'storeId', '')
      this.$set(this.ruleForm, 'dateData', [])
      this.$set(this.ruleForm, 'key', '')
    },
    handleDelete (row) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var params = {
          id: row.id
        }
        this.$store.dispatch('manage/UserDelete', params).then(res => {
          this.$message({
            type: 'success',
            message: res.msg
          })
          this.tablePage = 1
          this.handleList(this.ruleForm.storeId, this.selectForm.dateData[0], this.selectForm.dateData[1], this.selectForm.key)
        }).catch((err) => {
          this.$message({type: 'error', message: err.msg})
        })
      }).catch(() => {
      })
    },
    // 改变pagesize
    handleSizeChange (val) {
      this.page.pageSize = val
      this.handleList(this.ruleForm.storeId, this.selectForm.dateData[0], this.selectForm.dateData[1], this.selectForm.key)
    },
    // 改变页码
    handleCurrentChange (val) {
      this.tablePage = val
      this.handleList(this.ruleForm.storeId, this.selectForm.dateData[0], this.selectForm.dateData[1], this.selectForm.key)
    },
    // 表格选择
    handleSelectChange (val) {
      // console.log(val)
      var arr = []
      for (var i = 0; i < val.length; i++) {
        arr.push(val[i].id)
      }
      this.selectIds = arr
      // console.log(this.selectIds)
    },
    // 列表接口
    handleList (storeId, start, end, key) {
      this.tableLoading = true
      var params = {
        current: this.tablePage,
        end: end,
        ext: {},
        key: key,
        orders: [
          {
            order: 'desc',
            sort: 'createTime'
          }
        ],
        // signCourseId: 0,
        size: this.page.pageSize,
        start: start,
        storeId: storeId
      }
      this.$store.dispatch('manage/AppointmentPage', params).then(res => {
        // console.log(res)
        if (res.code == '200') {
          var data = res.data.records
          this.tableData = data
          this.page.total = parseInt(res.data.total)
          this.page.currentPage = this.tablePage
        }
        this.tableLoading = false
      }).catch(err => {
        console.log(err)
        this.tableLoading = false
      })
    },
    // 关闭弹窗
    closeDialog (params) {
      if (params == 'edit') {
        this.dialogIndexEdit = false
      }
    },
    // 确认
    confirmDialog (params) {
      if (params == 'edit') {
        this.dialogIndexEdit = false
      }
    },
    handleSearch () {
      this.selectForm.storeId = this.ruleForm.storeId
      this.selectForm.dateData = this.ruleForm.dateData ? this.ruleForm.dateData : []
      this.selectForm.key = this.ruleForm.key
      this.tablePage = 1
      this.handleList(this.ruleForm.storeId, this.selectForm.dateData[0], this.selectForm.dateData[1], this.selectForm.key)
    }
  }
}
</script>

<style lang="scss">
.pre-book-history {
  padding: 0px !important;
  height: auto !important;
  .btn-list {
    float: right;
    margin-right: 10px;
    font-size: 12px;
    color: #409EFF;
    cursor: pointer;
  }
  .el-card__header {
    border-bottom: none;
  }
  .el-card__header,
  .el-card__body {
    padding: 15px;
  }
  .fix-lh {
    line-height: 32px;
  }
  .el-input.is-disabled .el-input__inner {
    color: #606266;
  }
  .left{
    min-height:500px;
    background:#fff;
  }
  .left-tree{
    width: 100%;
    min-height:600px;
    overflow: auto;
  }
  .left-top{
    width: 100%;
    height: 40px;
    text-align: center;
    line-height: 40px;
    font-size: 16px;
    color: #333;
    background: #e2e2e2;
    font-size: 15px;
  }
}
</style>
